package org.me.marble;

import java.util.ArrayList;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 *
 * @author tungi
 */

public class LoadLevel{
    private int level_id;

    public LoadLevel(int level_id){
        //get the id of xml storing the values of the current level
        this.level_id=GameActivity.game.getResources().
                getIdentifier("level"+Integer.toString(level_id),"xml",
                GameActivity.game.getPackageName());
    }

    public void loadNextLevel(Light light,Path path,
            ArrayList<AvailableObject> objects,Fog fog)
        throws XmlPullParserException,IOException{
    	boolean normalLevel=OptionsLibrary.game_type.equals("normal");
    	//set some settings to default - see OptionsLibrary class decalration
    	OptionsLibrary.setNewLevel();
    	
        AvailableObject ao;

        XmlPullParser parser=GameActivity.game.getResources().getXml(level_id);
        int event=XmlPullParser.START_DOCUMENT;
        String tag="";
        while(event!=XmlPullParser.END_DOCUMENT){
            if(event==XmlPullParser.START_TAG){
                if(tag.equals("path"))
                    loadPath(path,parser);
                else if(tag.equals("light"))
                    loadLight(light,parser);
                else if(tag.equals("init"))
                	loadInit(fog,parser);
                else if(tag.equals("pointobject") && normalLevel){
                    objects.add(ao=new PointObject(gameObjects.PointObject.ordinal()));
                    Vector p=loadPoint(parser);
                    ao.centre=p;
                }
                else if(tag.equals("lifeobject") && normalLevel){
                    objects.add(ao=new LifeObject(gameObjects.LifeObject.ordinal()));
                    Vector p=loadPoint(parser);
                    ao.centre=p;
                }
                else if(tag.equals("fastmarkobject") && normalLevel){
                    objects.add(ao=new FastMarkObject(gameObjects.FastMarkObject.ordinal()));
                    Vector p=loadPoint(parser);
                    ao.centre=p;
                }
                else if(tag.equals("slowmarkobject") && normalLevel){
                    objects.add(ao=new SlowMarkObject(gameObjects.SlowMarkObject.ordinal()));
                    Vector p=loadPoint(parser);
                    ao.centre=p;
                }
                else if(tag.equals("invertmarkobject") && normalLevel){
                    objects.add(ao=new InvertMarkObject(gameObjects.InvertMarkObject.ordinal()));
                    Vector p=loadPoint(parser);
                    ao.centre=p;
                }
                else if(tag.equals("questionmarkobject") && normalLevel){
                    objects.add(ao=new QuestionMarkObject(gameObjects.QuestionMarkObject.ordinal()));
                    Vector p=loadPoint(parser);
                    ao.centre=p;
                }
                else if(tag.equals("gate")){
                    objects.add(ao=new Gate(gameObjects.Gate.ordinal()));
                    Vector p=loadPoint(parser);
                    ao.centre=p;
                }
            }
            event=parser.next();
            tag=parser.getName();
        }
        
        if(OptionsLibrary.game_type.equals("point")){
        	PointGenerator pg=new PointGenerator();
        	pg.generatePointObjects(objects,path.triangles,objects.get(objects.size()-1).getCentre());
        }
        else if(OptionsLibrary.game_type.equals("time")){
        	BlockGenerator bg=new BlockGenerator();
        	bg.generateBlocks(objects,path.triangles,objects.get(objects.size()-1).getCentre());
        }
        //change speed multiplication depends on the choosen difficulty level
        OptionsLibrary.speedMult+=0.1f*(float)OptionsLibrary.difficulty_level;
        OptionsLibrary.pointMult+=0.5f*(float)OptionsLibrary.difficulty_level;
    }
    
    public void loadInit(Fog fog,XmlPullParser parser)
    					throws XmlPullParserException,IOException{
        int event=parser.next();
        String tag=parser.getName();
    	while(true){
    		if(event==XmlPullParser.END_TAG && tag.equals("init"))
    			break;
    		if(event==XmlPullParser.START_TAG){
    			if(tag.equals("fog-density")){
    				parser.next();
                    OptionsLibrary.fog=true;
                    fog.setDensity(Float.parseFloat(parser.getText()));
    			}
    			else if(tag.equals("speed")){
    				parser.next();
                    OptionsLibrary.speedMult=Float.parseFloat(parser.getText());
    			}
    			else if(tag.equals("point-multiplication")){
    				parser.next();
                    OptionsLibrary.pointMult=Float.parseFloat(parser.getText());
    			}
    		}
    		event=parser.next();
            tag=parser.getName();
    	}
    }

    public void loadPath(Path path,XmlPullParser parser)
            throws XmlPullParserException,IOException{
        path.triangles.clear();
        int event=parser.next();
        String tag=parser.getName();
        while(true){
            if(event==XmlPullParser.START_TAG){
                if(tag.equals("triangle")){
                    loadTriangle(path,parser);
                }
                else if(tag.equals("color")){
                    MyColor color=loadColor(parser);
                    path.color=color;
                }
            }
            else if(event==XmlPullParser.END_TAG && tag.equals("path"))
                return;
        event=parser.next();
        tag=parser.getName();
        }
    }

    public void loadTriangle(Path path,XmlPullParser parser)
            throws XmlPullParserException,IOException{
        ArrayList<Integer> forejump=new ArrayList<Integer>();
        int backjump=1;
        Vector[] v=new Vector[3];
        int event=parser.next(),i=0;
        String tag=parser.getName();
        while(true){
            if(event==XmlPullParser.END_TAG && tag.equals("triangle")){
                path.triangles.add(new Triangle(v[0],v[1],v[2],forejump,backjump));
                return;
            }
            else if(event==XmlPullParser.START_TAG && tag.equals("point")){
                v[i++]=loadPoint(parser);
            }
            else if(event==XmlPullParser.START_TAG && tag.equals("forejump")){
                parser.next();
                forejump.add(Integer.parseInt(parser.getText()));
            }
            else if(event==XmlPullParser.START_TAG && tag.equals("backjump")){
                parser.next();
                backjump=Integer.parseInt(parser.getText());
            }
            event=parser.next();
            tag=parser.getName();
        }
    }

    public void loadLight(Light light,XmlPullParser parser)
            throws XmlPullParserException,IOException{
        int event=parser.next();
        String tag=parser.getName();
        while(true){
            if(event==XmlPullParser.START_TAG && tag.equals("point")){
                Vector v=loadPoint(parser);
                light._position=v;
            }
            else if(event==XmlPullParser.START_TAG && tag.equals("color")){
                MyColor color=loadColor(parser);
                light._color=color;
            }
            else if(event==XmlPullParser.END_TAG && tag.equals("light"))
                return;
            event=parser.next();
            tag=parser.getName();
        }
    }

    public Vector loadPoint(XmlPullParser parser) throws XmlPullParserException,IOException{
        Vector v=new Vector();
        int event=parser.next();
        String tag=parser.getName();
        while(true){
            if(event==XmlPullParser.START_TAG && tag.equals("x")){
                parser.next();
                v.setX(Float.parseFloat(parser.getText()));
            }
            else if(event==XmlPullParser.START_TAG && tag.equals("y")){
                parser.next();
                v.setY(Float.parseFloat(parser.getText()));
            }
            else if(event==XmlPullParser.START_TAG && tag.equals("z")){
                parser.next();
                v.setZ(Float.parseFloat(parser.getText()));
            }
            else if(event==XmlPullParser.END_TAG && tag.equals("point"))
                break;
            event=parser.next();
            tag=parser.getName();
        }
        return v;
    }

    public MyColor loadColor(XmlPullParser parser) throws XmlPullParserException,IOException{
        int event=parser.next();
        String tag=parser.getName();
        MyColor color=new MyColor();
        while(true){
            if(event==XmlPullParser.START_TAG && tag.equals("ambient"))
                color.setAmbient(loadRGBA(parser));
            else if(event==XmlPullParser.START_TAG && tag.equals("diffuse"))
                color.setDiffuse(loadRGBA(parser));
            else if(event==XmlPullParser.START_TAG && tag.equals("emission"))
                color.setEmission(loadRGBA(parser));
            else if(event==XmlPullParser.START_TAG && tag.equals("specular"))
                color.setSpecular(loadRGBA(parser));
            else if(event==XmlPullParser.START_TAG && tag.equals("shininess")){
                parser.next();
                color.setShininess(Float.parseFloat(parser.getText()));
            }
            else if(event==XmlPullParser.END_TAG && tag.equals("color"))
                break;
            event=parser.next();
            tag=parser.getName();
        }
        return color;
    }

    public float[] loadRGBA(XmlPullParser parser) throws XmlPullParserException,IOException{
        float[] color=new float[4];
        int event=parser.next();
        String tag=parser.getName();
        while(true){
            if(event==XmlPullParser.START_TAG && tag.equals("r")){
                parser.next();
                color[0]=Float.parseFloat(parser.getText());
            }
            else if(event==XmlPullParser.START_TAG && tag.equals("g")){
                parser.next();
                color[1]=Float.parseFloat(parser.getText());
            }
            else if(event==XmlPullParser.START_TAG && tag.equals("b")){
                parser.next();
                color[2]=Float.parseFloat(parser.getText());
            }
            else if(event==XmlPullParser.START_TAG && tag.equals("a")){
                parser.next();
                color[3]=Float.parseFloat(parser.getText());
                return color;
            }
            event=parser.next();
            tag=parser.getName();
        }
    }
}